"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 函数gms_random.string()返回指定模式和指定长度的随机字符串
Create At   : 2023/12/20
Owner       : @zou_jialiang050
Description :
    1.返回长度为6的大写字母的字符串：select gms_random.string(’u‘,6);
    2.返回长度为6的小写字母的字符串：select gms_random.string(’L‘,6);
    3.返回长度为6的大小写字母的字符串：select gms_random.string(’a‘,6);
    4.返回长度为6的大写字母和数字混合的字符串：select gms_random.string(’x‘,6);
    5.返回长度为6的所有可显示的字符：select gms_random.string(’p‘,6);
    6.多参数select gms_random.string('u', 8,9);
    7.长度字符类型字符串select gms_random.string('u','a');
Expect      :
    1.成功，生成的随机字符串不相同
    2.成功，生成的随机字符串不相同
    3.成功，生成的随机字符串不相同
    4.成功，生成的随机字符串不相同
    5.成功，生成的随机字符串不相同
    6.失败
    7.失败
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname = 'db_whale_44'
    
    def test_gms_random(self):
        text = "---pre1:创建A兼容性数据库;expect:成功---"
        self.logger.info(text)
        pre_cmd1 = f"drop database if exists {self.dbname};" \
            f"create database {self.dbname} dbcompatibility='A';"
        pre_msg1 = self.sh_primy.execut_db_sql(pre_cmd1)
        self.logger.info(pre_msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, pre_msg1,
                      text + '执行失败')
        text = "---pre2:在A数据库中创建whale插件;expect:成功---"
        self.logger.info(text)
        pre_cmd2 = f"create extension whale;"
        pre_msg2 = self.sh_primy.execut_db_sql(pre_cmd2, dbname=self.dbname)
        self.logger.info(pre_msg2)
        self.assertIn(self.constant.extension_create_success, pre_msg2,
                      text + '执行失败')
        
        text = "---step1:返回长度为6的大写字母的字符串;expect:成功---"
        self.logger.info(text)
        i = 0
        items = []
        while i < 2:
            sql_cmd1 = f"select gms_random.string('u',6);"
            msg1 = self.sh_primy.execut_db_sql(sql_cmd1, dbname=self.dbname)
            self.logger.info(msg1)
            item = msg1.splitlines()[-2]
            items.append(item)
            i += 1
        items1 = items[0].strip()
        items2 = items[1].strip()
        self.assertNotEqual(items[0], items[1], text + '执行失败')
        self.assertTrue(items1.isupper() == items2.isupper() == True
                        and len(items1) == len(items2) == 6)

        text = "---step2:返回长度为6的小写字母的字符串;expect:成功---"
        self.logger.info(text)
        i = 0
        items = []
        while i < 2:
            sql_cmd2 = f"select gms_random.string('L',6);"
            msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname)
            self.logger.info(msg2)
            item = msg2.splitlines()[-2]
            items.append(item)
            i += 1
        items1 = items[0].strip()
        items2 = items[1].strip()
        self.assertNotEqual(items[0], items[1], text + '执行失败')
        self.assertTrue(items1.islower() == items2.islower() == True
                        and len(items1) == len(items2) == 6)

        text = "---step3:返回长度为6的大小写字母的字符串;expect:成功---"
        self.logger.info(text)
        i = 0
        items = []
        while i < 2:
            sql_cmd3 = f"select gms_random.string('a',6);"
            msg3 = self.sh_primy.execut_db_sql(sql_cmd3, dbname=self.dbname)
            self.logger.info(msg3)
            item = msg3.splitlines()[-2]
            items.append(item)
            i += 1
        items1 = items[0].strip()
        items2 = items[1].strip()
        self.assertNotEqual(items[0], items[1], text + '执行失败')
        self.assertTrue(items1.islower() == items2.isupper() == False
                        and len(items1) == len(items2) == 6)

        text = "---step4:返回长度为6的大写字母和数字混合的字符串;expect:成功---"
        self.logger.info(text)
        i = 0
        items = []
        while i < 2:
            sql_cmd4 = f"select gms_random.string('X',6);"
            msg4 = self.sh_primy.execut_db_sql(sql_cmd4, dbname=self.dbname)
            self.logger.info(msg4)
            item = msg4.splitlines()[-2]
            items.append(item)
            i += 1
        items1 = items[0].strip()
        items2 = items[1].strip()
        self.assertNotEqual(items[0], items[1], text + '执行失败')
        self.assertTrue(items1.isalnum() == items2.isalnum() == True
                        and len(items1) == len(items2) == 6)
                
        text = "---step5:返回长度为6的所有可显示的字符;expect:成功---"
        self.logger.info(text)
        i = 0
        items = []
        while i < 2:
            sql_cmd5 = f"select gms_random.string('p',6);"
            msg5 = self.sh_primy.execut_db_sql(sql_cmd5, dbname=self.dbname)
            self.logger.info(msg5)
            item = msg5.splitlines()[-2]
            items.append(item)
            i += 1
        items1 = items[0].strip()
        items2 = items[1].strip()
        self.assertNotEqual(items[0], items[1], text + '执行失败')
        self.assertTrue(items1.isalnum() == items2.isalnum() == False
                        and len(items1) == len(items2) == 6)

        text = "---step6:gms_random.string函数多参;expect:成功---"
        self.logger.info(text)
        sql_cmd6 = f"select gms_random.string('u',6,7);"
        msg6 = self.sh_primy.execut_db_sql(sql_cmd6, dbname=self.dbname)
        self.logger.info(msg6)
        self.assertIn('function gms_random.string(unknown, integer, integer) '
                      'does not exist', msg6, text + '执行失败')

        text = "---step6:gms_random.string函数第二个参数为字符串;expect:成功---"
        self.logger.info(text)
        sql_cmd6 = f"select gms_random.string('u','g');"
        msg6 = self.sh_primy.execut_db_sql(sql_cmd6, dbname=self.dbname)
        self.logger.info(msg6)
        self.assertIn('invalid input syntax for integer', msg6,
                      text + '执行失败')

    def tearDown(self):
        text = '----清理环境----'
        self.logger.info(text)
        clean_cmd = f"drop database {self.dbname};"
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_msg,
                      text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
